package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.LocationData;
import com.qiantu.phone.ui.activity.map.LocationActivity;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.fragment.device.EightKeyFragment;
import com.qiantu.phone.ui.fragment.device.FourKeyFragment;
import com.qiantu.phone.ui.fragment.device.OneKeyFragment;
import com.qiantu.phone.ui.fragment.device.SixKeyFragment;
import com.qiantu.phone.ui.fragment.device.ThreeKeyFragment;
import com.qiantu.phone.ui.fragment.device.TwoKeyFragment;
import com.qiantu.phone.ui.uiutil.DeviceType;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;
import com.qiantu.phone.utils.LinkageDataHolder;
import com.qiantu.phone.widget.SectionSeekBar;
import com.qiantu.phone.widget.StatusSeekBar;
import com.qiantu.phone.widget.btnview.OnBtnClickListener;
import com.qiantu.phone.widget.btnview.OneTwoThreeFourSixEightNineButtonView;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class LinkagePanelActivity extends AppActivity {
    private SettingBar settingAction;
    private SettingBar settingDataSourceDevice;
    private TextView tvTemperatureCondition;
    private TextView tvSettingText;
    private String serialNo;
    private String areaName;
    private String areaCoding;
    private double latitude;
    private double longitude;
    private int scopeLong;
    private DeviceBean device;
    private LinkageBean mLinkageBean;
    private LinkageBean.LinkageCondition mLinkageCondition;
    private BottomListDialog mBottomActionDialog;
    private BottomListDialog mBottomPanelActionDialog;
    private OneTwoThreeFourSixEightNineButtonView oneButtonView;
    private OneTwoThreeFourSixEightNineButtonView twoButtonView;
    private OneTwoThreeFourSixEightNineButtonView threeButtonView;
    private OneTwoThreeFourSixEightNineButtonView fourButtonView;
    private OneTwoThreeFourSixEightNineButtonView sixButtonView;
    private OneTwoThreeFourSixEightNineButtonView eightButtonView;


    private int action = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_linkage_panel;
    }

    @Override
    protected void initView() {
        settingAction = findViewById(R.id.setting_action);
        settingDataSourceDevice = findViewById(R.id.setting_data_source_device);
        tvSettingText = findViewById(R.id.tv_setting_text);
        tvTemperatureCondition = findViewById(R.id.tv_temperature_condition);
        oneButtonView = findViewById(R.id.one_btn_view);
        twoButtonView = findViewById(R.id.two_btn_view);
        threeButtonView = findViewById(R.id.three_btn_view);
        fourButtonView = findViewById(R.id.four_btn_view);
        sixButtonView = findViewById(R.id.six_btn_view);
        eightButtonView = findViewById(R.id.eight_btn_view);
        OnBtnClickListener listener = new OnBtnClickListener() {
            @Override
            public void onClick(int btnID) {
                showBottomActionDialog(btnID);
            }
        };
        oneButtonView.setOnBtnClickListener(listener);
        twoButtonView.setOnBtnClickListener(listener);
        threeButtonView.setOnBtnClickListener(listener);
        fourButtonView.setOnBtnClickListener(listener);
        sixButtonView.setOnBtnClickListener(listener);
        eightButtonView.setOnBtnClickListener(listener);
    }

    @Override
    protected void initData() {
        serialNo = getString("serial_no");
        areaCoding = getString("room_serial_no");
        areaName = getString("floor_room_name");
        int conditionType = getInt("environmentalType");
        String deviceSerialNo = getString("device_serial_no");
        mLinkageBean = LinkageDataHolder.getLinkageData();
        List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
        if (linkageConditions != null && linkageConditions.size() > 0 && !TextUtils.isEmpty(serialNo)) {
            for (LinkageBean.LinkageCondition item : linkageConditions) {
                if (item.getConditionSerialNo().equals(serialNo)) {
                    mLinkageCondition = item;
                    deviceSerialNo = mLinkageCondition.getDeviceSerialNo();
                    areaName = mLinkageCondition.getAreaName();
                    areaCoding = mLinkageCondition.getAreaCoding();
                    latitude = mLinkageCondition.getLatitude();
                    longitude = mLinkageCondition.getLongitude();
                    scopeLong = mLinkageCondition.getScopeLong();
                    break;
                }
            }
        }

        device = LLDeviceDBManager.getInstance(getContext()).getDeviceBySerialNo(deviceSerialNo);
        if(mLinkageCondition == null){
            mLinkageBean.setLinkageConditions(linkageConditions);
            mLinkageCondition = new LinkageBean.LinkageCondition();
            mLinkageCondition.setConditionSerialNo(UUID.randomUUID().toString());
            mLinkageCondition.setConditionType(conditionType);
            mLinkageCondition.setLogicType(1);
            mLinkageCondition.setAreaType(getBoolean("is_indoor") ? 1 : 2);
            mLinkageCondition.setDeviceSerialNo(getString("device_serial_no"));
//            mLinkageCondition.setImageUrl(LLSceneLinkageUtil.getLinkageImageStr(mLinkageCondition.getConditionType()));
            mLinkageCondition.setBriefing(device.getName());
//            mLinkageCondition.setStateName(LLSceneLinkageUtil.getLinkageState(mLinkageCondition.getConditionType()));

            linkageConditions.add(mLinkageCondition);
            mLinkageCondition.setSort(linkageConditions.indexOf(mLinkageCondition) + 1);
        }
        updateView();
    }

    private void updateView() {
        settingAction.setRightDrawable(null);
        settingAction.setRightText("按键变为按压时");
        settingDataSourceDevice.setRightText(device.getName());
        setOnClickListener(settingDataSourceDevice);
        settingDataSourceDevice.setRightDrawable(null);
        settingDataSourceDevice.setEnabled(false);
        tvSettingText.setText("选择按键");
        settingAction.setRightText(action == 0 ? R.string.linkage_action_become : R.string.linkage_action_keep);


        String defaultName = AppApplication.getInstance().getResources().getString(R.string.custom_name);
        String key1 = defaultName;
        String key2 = defaultName;
        String key3 = defaultName;
        String key4 = defaultName;
        String key5 = defaultName;
        String key6 = defaultName;
        String key7 = defaultName;
        String key8 = defaultName;
        if (device.getDeviceCustomKeys() != null) {
            for (DeviceCustomKeyBean keyData : device.getDeviceCustomKeys()) {
                switch (keyData.getKeyIndex()) {
                    case "1":
                        key1 = keyData.getKeyName();
                        break;
                    case "2":
                        key2 = keyData.getKeyName();
                        break;
                    case "3":
                        key3 = keyData.getKeyName();
                        break;
                    case "4":
                        key4 = keyData.getKeyName();
                        break;
                    case "5":
                        key5 = keyData.getKeyName();
                        break;
                    case "6":
                        key6 = keyData.getKeyName();
                        break;
                    case "7":
                        key7 = keyData.getKeyName();
                        break;
                    case "8":
                        key8 = keyData.getKeyName();
                        break;
                }
            }
        }
        if (device.getDeviceType().equals(DeviceType.WALL_SWITCH1.stringType)||device.getDeviceType().equals(DeviceType.WALL_SWITCH_S1.stringType)) {
            oneButtonView.setVisibility(View.VISIBLE);
            oneButtonView.setText(key1,null);
        } else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH2.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S2.stringType)) {
            twoButtonView.setVisibility(View.VISIBLE);
            twoButtonView.setText(key1,key2);
        } else if(device.getDeviceType().equals(DeviceType.WALL_SWITCH3.stringType)|| device.getDeviceType().equals(DeviceType.WALL_SWITCH_S3.stringType)){
            threeButtonView.setVisibility(View.VISIBLE);
            threeButtonView.setText(key1,key2,key3);
        }else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH4.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_J4.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S4.stringType)) {
            fourButtonView.setVisibility(View.VISIBLE);
            fourButtonView.setText(key1,key2,key3,key4);
        } else if(device.getDeviceType().equals(DeviceType.WALL_SWITCH6.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S6.stringType)){
            sixButtonView.setVisibility(View.VISIBLE);
            sixButtonView.setText(key1,key2,key3,key4,key5,key6,null,null);
        } else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH8.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_J8.stringType)) {
            eightButtonView.setVisibility(View.VISIBLE);
            eightButtonView.setText(key1,key2,key3,key4,key5,key6,key7,key8);
        }
    }

    @Override
    public void onRightClick(View view) {
        if (mLinkageCondition.getAreaType() != 1) {
            mLinkageCondition.setLongitude(longitude);
            mLinkageCondition.setLatitude(latitude);
            mLinkageCondition.setScopeLong(scopeLong);
        }
        mLinkageCondition.setAreaName(areaName);
        mLinkageCondition.setAreaCoding(areaCoding);
        startActivity_SingleTask(EditLinkageActivity.class);
        finish();
    }

    private void showBottomActionDialog(int btnID) {
        if (mBottomPanelActionDialog == null) {
            mBottomPanelActionDialog = new BottomListDialog(this);
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add("单击");
            data.add("双击");
            data.add("三击");
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    mBottomPanelActionDialog.dismiss();
                }
            });
            mBottomPanelActionDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomPanelActionDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomPanelActionDialog.getAdapter();
        listLabelAdapter.setSelected("");
        mBottomPanelActionDialog.show();
    }
}